import java.util.LinkedList;
import java.util.List;

/**
 * Creat with IntelliJ IDEA
 * Description:实现单向链表(不带傀儡节点)代码.
 * User:WZW
 * Date:2021-11-03
 * Time:7:29
 */
class ListNode{
    int val;
    ListNode next;
    ListNode(int val){
        this.val = val;
    }
}

public class MyLinkedList {
    public ListNode head;

    public void create(){//枚举法
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        this.head = listNode1;
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
    }

    public void display(){
        ListNode cur = head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }

    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        //链表为空
        if(this.head==null){
            this.head = node;
            return;
        }
        //链表不为空
        ListNode cur = this.head;
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=node;
    }

    public ListNode findIndex(int index){
        ListNode cur = this.head;
        while(index-1!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        //判断坐标合法性
        if(index<0||index>size()){
            System.out.println("你输入的位置有误！");
            return;
        }
        if(index==0){
            //头插
            addFirst(data);
            return;
        }
        if(index==size()){
            addLast(data);
            return;
        }
        //中间
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
        node.next=cur.next;
        cur.next=node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    //找到要删除的节点的前一个节点
    public ListNode findNode(int key){
        ListNode cur = this.head;
        while (cur.next!=null){
            if(cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head==null){
            System.out.println("链表为空，没有可删除的节点！");
            return;
        }
        if (head.val==key){
            head=head.next;
            return;
        }
        ListNode cur = findNode(key);
        if (cur==null){
            System.out.println("没有你要删除的节点！");
            return;
        }
        //cur.next=(cur.next).next;
        ListNode del = cur.next;
        cur.next=del.next;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head==null){
            System.out.println("链表为空，没有可删除的节点！");
            return;
        }
        ListNode cur = head;
        ListNode del = head.next;
        while (del!=null){
            if (del.val==key){
                cur.next=del.next;
            }else {
                cur=del;
            }
            del=del.next;
        }
        if(head.val==key){
            head=head.next;
        }
    }

    //清空单链表
    //博哥的版本：
    public void clear(){
        while (head!=null){
            ListNode next = head.next;
            head.next = null;
            head = next;
        }
    }

    //反转链表 -  反转一个单链表
    public ListNode reverseList(ListNode head){
        if (this.head==null){
            return null;
        }
        ListNode cur = head;
        ListNode pre = null;
        while(cur!=null){
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }

    //输入一个链表，输出该链表中倒数第k个结点。
    public ListNode findK(ListNode head,int k){
        if(head==null||k<=0){
            return null;
        }else {
            ListNode fast = head;
            ListNode slow = head;
            for (int i = 0; i < k-1; i++) {
                fast = fast.next;
                if(fast==null){
                        return null;
                    }
            }
            while (fast.next!=null){
                fast = fast.next;
                slow = slow.next;
            }
            return slow;
        }
    }

    public boolean isHuiWen(ListNode head){
        if(head==null){
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast!=null && fast.next!=null){//要判断fast也不为空
            fast = fast.next.next;
            slow = slow.next;
        }
        //reverse后半段
        ListNode cur = slow.next;
        while(cur!=null){
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;//这里之前写错了，写成了cur = cur.next
            cur = curNext;
        }
        //判断回纹
        while (head!=slow && head.next!=slow){
            if(head.val== slow.val){
                head = head.next;
                slow = slow.next;
            }else {
                return false;
            }
        }
        return true;
    }

    //自己的版本：
//    public void clear(){
//        if (head==null){
//            System.out.println("单链表已经为空！");
//            return;
//        }
//        //head = null;//粗暴法
//        ListNode del = head;
//        while (del!=null){
//            head.next = del.next;
//            del = del.next;
//        }
//        head = null;
//    }
}
